home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Text⁄Files / FaceLift / FaceLift Folder / FLConvOps.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-16  |  4.4 KB  |  237 lines  |  [TEXT/KAHL]

  1. # include    "FLMapInfo.h"
  2. # include    "FLMaca.h"
  3. # include    "FaceLift.h"
  4.  
  5.  
  6. static void
  7. CSpecToCStr (Boolean mark, ConvSpec *cSpec, ConvStr *cStr, StringPtr defStr)
  8. {
  9.     MarkToStr (mark, cStr->markStr);
  10.     FontToStr (cSpec->font, cStr->fontStr, defStr);
  11.     SizeToStr (cSpec->size, cStr->sizeStr, defStr);
  12.     StyleToStr (cSpec->style, cStr->styleStr, defStr);
  13. }
  14.  
  15.  
  16. void
  17. ClearCSpec (ConvSpec *cSpec)
  18. {
  19.     cSpec->font = anyFont;
  20.     cSpec->size = anySize;
  21.     cSpec->style = anyStyle;
  22. }
  23.  
  24.  
  25. void
  26. ClearMSpec (MapSpec *mSpec)
  27. {
  28.     ClearCSpec (&mSpec->inFmt);
  29.     ClearCSpec (&mSpec->outFmt);
  30.     mSpec->isInput = true;
  31. }
  32.  
  33.  
  34. /*
  35.  * Compare two conversion specifications.
  36.  * Return:
  37.  *        0        c1 = c2
  38.  *        < 0        c1 < c2
  39.  *        > 0        c1 > c2
  40.  *
  41.  * Font sorts before size, which sorts before style.
  42.  *
  43.  * For all of font, size and style, the "any" selection is greater,
  44.  * so that generic selections sort to the end of the list.  Font
  45.  * comparisons are otherwise based on the name of the font, rather
  46.  * than its number.   A string compare is avoided by keeping the
  47.  * fonts in alphabetic order in the font information structures.
  48.  *
  49.  * Note that the style comparison does not use the manifest constants
  50.  * defined for each style value, and so would break if style coding
  51.  * changed!
  52.  */
  53.  
  54. short
  55. CompareCSpec (ConvSpec *c1, ConvSpec *c2)
  56. {
  57. int    i;
  58. int    s1, s2;
  59.  
  60.     if (c1->font != c2->font)
  61.     {
  62.         if (c1->font == anyFont)
  63.             return (1);
  64.         if (c2->font == anyFont)
  65.             return (-1);
  66.         return (FontIndex (c1->font) - FontIndex (c2->font));
  67.     }
  68.  
  69.     if ((i = c1->size - c2->size) != 0)
  70.     {
  71.         if (c1->size == anySize)
  72.             return (1);
  73.         if (c2->size == anySize)
  74.             return (-1);
  75.         return (i);
  76.     }
  77.  
  78.     s1 = c1->style;
  79.     s2 = c2->style;
  80.     if (s1 - s2 != 0)
  81.     {
  82.         if (s1 == anyStyle)
  83.             return (1);
  84.         if (s2 == anyStyle)
  85.             return (-1);
  86.         for (i = 0; i < 7; ++i)
  87.         {
  88.             if (s1 == 0)
  89.             {
  90.                 if (s2 != 0)
  91.                     return (-1);
  92.             }
  93.             else if (s2 == 0)
  94.             {
  95.                 return (1);
  96.             }
  97.             else if (s1 & 1)
  98.             {
  99.                 if (!(s2 & 1))
  100.                     return (-1);
  101.             }
  102.             else if (s2 & 1)
  103.                 return (1);
  104.             s1 >>= 1;
  105.             s2 >>= 1;
  106.         }
  107.     }
  108.     return (0);            /* equal */
  109. }
  110.  
  111.  
  112. /*
  113.  * Convert map specs to map text.  Do this by converting input specs
  114.  * and output specs.
  115.  */
  116.  
  117. void
  118. MSpecToMStr (MapSpec *mSpec, MapStr *mStr)
  119. {
  120.     CSpecToCStr (mSpec->isInput, &mSpec->inFmt, &mStr->inStr, "\pAny");
  121.     CSpecToCStr (!mSpec->isInput, &mSpec->outFmt, &mStr->outStr, "\pSame");
  122. }
  123.  
  124.  
  125. /*
  126.  * Set the mark field with a bullet if the value is true.
  127.  */
  128.  
  129. void
  130. MarkToStr (Boolean value, StringPtr str)
  131. {
  132.     CopyString (value ? "\p\245" : "\p", str);
  133. }
  134.  
  135.  
  136. /*
  137.  * The font *must* be legal
  138.  */
  139.  
  140. void
  141. FontToStr (int font, StringPtr str, StringPtr defStr)
  142. {
  143.     if (font == anyFont)        /* Any/Same */
  144.         CopyString (defStr, str);
  145.     else
  146.         FontName (FontIndex (font), str);
  147. }
  148.  
  149.  
  150. void
  151. SizeToStr (int size, StringPtr str, StringPtr defStr)
  152. {
  153.     if (size == anySize)        /* no size specified, use default */
  154.         CopyString (defStr, str);
  155.     else
  156.         NumToString ((long) size, str);
  157. }
  158.  
  159.  
  160. /*
  161.  * Convert style value to string. If the style has only one attribute
  162.  * bit set, use the attribute name, otherwise use abbreviated form with
  163.  * one letter for each attribute.
  164.  */
  165.  
  166. static StringPtr    styleStr[7] =
  167. {
  168.     (StringPtr) "\pBold",
  169.     (StringPtr) "\pItalic",
  170.     (StringPtr) "\pUnder",
  171.     (StringPtr) "\pOutline",
  172.     (StringPtr) "\pShadow",
  173.     (StringPtr) "\pHigh",
  174.     (StringPtr) "\pLow"
  175. };
  176.  
  177.  
  178. void
  179. StyleToStr (int style, StringPtr str, StringPtr defStr)
  180. {
  181. int    i, style2;
  182.  
  183.     if (style == anyStyle)        /* no style specified, use default */
  184.         CopyString (defStr, str);
  185.     else if (style == 0)            /* no bits set = plain text */
  186.         CopyString ("\pPlain", str);
  187.     else
  188.     {
  189.         style2 = style;
  190.         for (i = 0; i < 7; ++i)
  191.         {
  192.             if ((style2 & 1))
  193.             {
  194.                 if (style2 != 1)
  195.                     break;            /* more than 1 bit set */
  196.                 CopyString (styleStr[i], str);
  197.                 return;
  198.             }
  199.             style2 >>= 1;
  200.         }
  201.         i = 0;
  202.         if (style & styleBold)
  203.             str[++i] = 'B';
  204.         if (style & styleItalic)
  205.             str[++i] = 'I';
  206.         if (style & styleUnder)
  207.             str[++i] = 'U';
  208.         if (style & styleOutline)
  209.             str[++i] = 'O';
  210.         if (style & styleShadow)
  211.             str[++i] = 'S';
  212.         if (style & styleSuper)
  213.             str[++i] = 'H';
  214.         if (style & styleSub)
  215.             str[++i] = 'L';
  216.         str[0] = i;
  217.     }
  218. }
  219.  
  220.  
  221. /*
  222.  * Convert document format to map spec.  The format is put in the
  223.  * input side of the map spec.  The format is assumed to be legal.
  224.  */
  225.  
  226. void
  227. FormatToMSpec (Format *fmt, MapSpec *mSpec)
  228. {
  229. ConvSpec    *cSpec;
  230.  
  231.     ClearMSpec (mSpec);
  232.     cSpec = &mSpec->inFmt;
  233.     cSpec->size = fmt->fmtSize;
  234.     cSpec->style = fmt->fmtStyle;
  235.     cSpec->font = fmt->fmtFont;
  236. }
  237.